# Makefile - Top-level makefile.
# Copyright (C) 2007  Mikhail Vorozhtsov
# See the LICENSE section of the README file for details.

# $Id$

ifndef SBE_BASE
  $(error SBE_BASE is not defined)
endif

include $(SBE_BASE)/mk/sbe.mk

$(call sbe-init,0,0,machine)
$(call sbe-env-fns,anima)

$(call sbe-dep,libcompiler,0,0)
$(call sbe-dep,libbst,0,0)

override CPPFLAGS := \
  $(CPPFLAGS) \
  $(SBE_CPPFLAGS) \
  -nostdinc \
  -I headers \
  -include "headers/build/env.hh" \
  $(call sbe-dep-var,libbst,CPPFLAGS) \
  -DLIBBST_MJ=$(call sbe-dep-var,libbst,VER_MAJOR) \
  -DLIBBST_NS=bst_$(call sbe-dep-var,libbst,VER_MAJOR) \
  -DLIBBST_PREFIX=bst-$(call sbe-dep-var,libbst,VER_MAJOR)
override CXXCPPFLAGS := \
  $(CXXCPPFLAGS) $(call sbe-dep-var,libcompiler,CXXCPPFLAGS)
override CXXFLAGS := \
  $(CXXFLAGS) \
  -std=gnu++98 \
  -fno-threadsafe-statics \
  -fno-builtin -fno-rtti -fno-exceptions \
  -Wall -Wextra -Wno-pointer-arith -Wno-invalid-offsetof -Wno-parentheses \
  -Werror
override LDFLAGS := $(LDFLAGS) -static -n

IMAGE := anima.$(ARCH)-$(PLATFORM)

LD_SCRIPT := \
  $(call if-file-exists,sources/anima/$(ARCH)/$(PLATFORM)/anima.lds, \
                        sources/anima/$(ARCH)/anima.lds)

ASM_SRCS :=
CXX_SRCS := \
  $(call anima-src,init.cc cmdline.cc console.cc firmware.cc pdb.cc mm.cc) \
  $(call anima-src,cpu.cc space.cc thread.cc)

$(call sbe-machine-init)

ASM_OBJS := $(call obj-files,$(ASM_SRCS))
ASM_DEPS := $(ASM_OBJS:%.o=%.d)

CXX_OBJS := $(call obj-files,$(CXX_SRCS))
CXX_DEPS := $(CXX_OBJS:%.o=%.d)

default: all

ifneq ($(MAKECMDGOALS),clean)
-include $(ASM_DEPS)
-include $(CXX_DEPS)
endif

$(call asm-rules,$(ASM_SRCS),$(ASM_OBJS))
$(call cxx-rules,$(CXX_SRCS),$(CXX_OBJS))

$(IMAGE): $(LD_SCRIPT) $(ASM_OBJS) $(CXX_OBJS)
	cat $(LD_SCRIPT) \
	  | $(CPP) -DIN_LDS_CODE $(CPPFLAGS) - \
	  | grep -v '^#' \
	  | $(LD) $(LDFLAGS) -T /dev/stdin -o $(IMAGE) $(ASM_OBJS) $(CXX_OBJS)

.PHONY: all
all: $(IMAGE)

.PHONY: clean
clean:
	rm -f $(IMAGE) $(ASM_OBJS) $(ASM_DEPS) $(CXX_OBJS) $(CXX_DEPS)

